/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the seckill4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.jalonjia.seckill.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jalonjia.seckill.admin.api.entity.SysDictItem;
import com.jalonjia.seckill.common.security.util.SecurityUtils;
import com.jalonjia.seckill.core.entity.SeckillOrder;
import com.jalonjia.seckill.core.entity.SeckillTarget;
import com.jalonjia.seckill.core.mapper.SeckillOrderMapper;
import com.jalonjia.seckill.core.mapper.SeckillTargetMapper;
import com.jalonjia.seckill.core.service.SeckillOrderService;
import com.jalonjia.seckill.core.service.SeckillTargetService;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 秒杀对象表
 *
 * @author 贾晓超
 * @date 2022-05-05 20:56:44
 */
@Service
public class SeckillTargetServiceImpl extends ServiceImpl<SeckillTargetMapper, SeckillTarget> implements SeckillTargetService {
	@Resource
	private SeckillTargetMapper seckillTargetMapper;
	@Resource
	private SeckillOrderMapper seckillOrderMapper;

	@Override
	public List<SysDictItem> getTargetDict() {
		List<SeckillTarget> seckillTargets;
		if (SecurityUtils.isAdmin()) {
			seckillTargets = query().list();
		} else {
			// 查寻用户创建的和用户参与的
			List<Long> orderList = seckillOrderMapper.selectList(new QueryWrapper<SeckillOrder>().eq("create_by", SecurityUtils.username()))
					.stream().map(SeckillOrder::getTargetNo).collect(Collectors.toList());
			seckillTargets = query().eq("create_by", SecurityUtils.getUser().getUsername())
					.or().in("id", orderList).list();
		}
		return seckillTargets.stream().map(target -> {
			SysDictItem sysDictItem = new SysDictItem();
			sysDictItem.setValue(target.getId().toString());
			sysDictItem.setLabel(target.getTitle());
			sysDictItem.setType("targetDicts");
			return sysDictItem;
		}).collect(Collectors.toList());
	}

	@Override
	public boolean reduceRemainNum(Long id) {
		return seckillTargetMapper.reduceRemainNum(id) > 0;
	}
}
